package cn.wolfcode.p2p.base.util;

import cn.wolfcode.p2p.base.exception.CustomerException;

import java.util.Date;

/**
    断言工具类
 */
public enum AssertUtil {
    INSTANCE;

    /**
        判断字符串是否为空, 和是否为空字符串
     */
    public AssertUtil isNotEmpty(String str, String errorMsg){
        if (str == null || str.trim().length() == 0){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断是否为手机号码格式
     */
    public AssertUtil isPhoneNumber(String phoneNumber, String errorMsg){
        if (!phoneNumber.matches("^1[3|4|5|6|7|8|9][0-9]{9}$")){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断密码长度是否有效
     */
    public AssertUtil lenghtIn(String str, int minLenght, int maxLenght, String errorMsg){
        if (str.trim().length() < minLenght || str.trim().length() > maxLenght){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断是否存在相同的手机号码
     */
    public AssertUtil isFalse(boolean condition, String errorMsg){
        if (condition){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断是否为空对象
     */
    public AssertUtil isNullObject(Object obj, String errorMsg){
        if (obj == null){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断两个字符串是否相等
     */
    public AssertUtil isEquals(String str1, String str2, String errorMsg){
        if (!(str1.equals(str2))){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断两个字符串忽略大小写是否相等
     */
    public AssertUtil isEqualsIgnore(String str1, String str2, String errorMsg){
        if (!(str1.equalsIgnoreCase(str2))){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断两个时间是间隔是否大于某个值
     */
    public AssertUtil isBigger(Date date1, Date date2, int interval, String errorMsg){
        if (DateUtil.getSecondsBetween(date1, date2) > interval){
            throw new CustomerException(errorMsg);
        }
        return this;
    }

    /**
        判断两个时间是间隔是否小于某个值
     */
    public AssertUtil isSmaller(Date date1, Date date2, int interval, String errorMsg){
        if (DateUtil.getSecondsBetween(date1, date2) < interval){
            throw new CustomerException(errorMsg);
        }
        return this;
    }
}
